package com.xjj.stack;

import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

public class E0020ValidParentheses {

    public boolean isValid(String s) {
        Stack<Character> stack = new Stack<>();
        Map<Character, Character> map = new HashMap<>();
        map.put('(', ')');
        map.put('[', ']');
        map.put('{', '}');
        for (char ch : s.toCharArray()) {
            if (stack.empty() || map.get(stack.peek()) == null || map.get(stack.peek()) != ch) {
                stack.push(ch);
            } else {
                stack.pop();
            }
        }
        return stack.empty();
    }

}
